/*
 *  Copyright (C) 2011 Hatsites <Hatsites_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.shr.dtb.language.dictionary;

import org.myorg.shr.dtb.structure.db.table.TblAbs;
import org.myorg.shr.dtb.structure.dtb.tbl.fld.*;

/**
 *
 * @author Hatsites
 *
 * email: Hatsites_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */

public class DictionaryMysql {

    public static String formatField(String strField) {
       String str="";
       if (strField.isEmpty()){
           str = STR_NULL;
       }
       else {
            str=STR_QUOTE  + strField + STR_QUOTE ;
       }

       return str;
    }
    public static String createField(TblAbs tbl , int fieldIndex){

        FldTypAbs fldType = tbl.getFieldType(fieldIndex);

        if( fldType instanceof FldTypVarChar ){
            int size = tbl.getFieldSize(fieldIndex);
            String strName = tbl.getFieldName(fieldIndex);
            if (size < MysqlSettings.LENGTH_CHAR_MIN || size > MysqlSettings.LENGHT_CHAR_MAX){
                return "TODO";
            }
            else {
                return strName + STR_BLANK + TYP_VAR_CHAR +s_sz(size) + STR_FIELD_SEP;
            }
        }
        if (fldType instanceof FldTypChar){
            int size = tbl.getFieldSize(fieldIndex);
            String strName = tbl.getFieldName(fieldIndex);
            return strName + STR_BLANK + TYP_CHAR + s_sz(size) + STR_FIELD_SEP;
        }
        if (fldType instanceof FldTypId){
            return  tbl.getFieldName(fieldIndex) + STR_BLANK +
          TYP_ID + STR_FIELD_SEP;
        }
        if (fldType instanceof FldTypDate){
            String strName = tbl.getFieldName(fieldIndex);
            return strName + STR_BLANK + STR_TYP_DATE + STR_FIELD_SEP;
        }
        if (fldType instanceof FldTypRefId){
            return tbl.getFieldName(fieldIndex) + STR_BLANK +
                    TYP_REF_ID + STR_BLANK +
                    tbl.getReferenceTable(fieldIndex).getName() +
                    s_intoPar(tbl.getReferenceField(fieldIndex)) + STR_FIELD_SEP;
        }
        else{
            return "TODO"+ STR_FIELD_SEP;
        }
    }
    public static String createLastField(String strField, String strType){
        return strField + STR_BLANK + strType + STR_END_COMMAND;
    }
    public static String addField(String strField){
        return  formatField(strField) + STR_FIELD_SEP;
    }
    public static String addLastField(String strField){
         return formatField(strField) + STR_END_COMMAND ;
    }
    public static String countElements(String strTable){
        return STR_CMD_SELECT + STR_BLANK +
               STR_CMD_COUNT + s_intoPar(STR_STAR) + STR_BLANK +
               STR_FROM + STR_BLANK +
               strTable + STR_END;
    }
    public static String insertIntoTable(String strTable){
        return STR_CMD_INSERT + STR_BLANK +
               STR_INTO + STR_BLANK +
               strTable + STR_BLANK +
               STR_VALUES + STR_BLANK +
               STR_PAR_RIGHT;
    }
    public static String createDB(String strDB){
        return STR_CREATE_DTB + strDB  + STR_END;
    }

    public static String dropDB(String strDB){
        return STR_DROP_DTB + strDB;
    }

    public static String useDB(String strDB){
        return STR_CMD_USE + STR_BLANK +strDB;
    }

    public static String createTable(String strTable){
        return STR_CREATE_TBL + strTable + STR_PAR_RIGHT;
    }

    public static String dropTable(String strTable){
        return STR_DROP_TABLE + strTable;
    }


    /****************************************************************************
   *                  Definition of string for mysql types                    *
   ****************************************************************************/

    final static private String _STR_VAR = "VAR";

    final static public String TYP_CHAR = "CHAR";
    final static public String TYP_BINARY = "BINARY";

    final static public String TYP_VAR_CHAR = _STR_VAR + TYP_CHAR;
    final static public String TYP_VAR_BINARY = _STR_VAR + TYP_BINARY;

    final static private String _STR_TINY = "TINY";
    final static private String _STR_MEDIUM = "MEDIUM";
    final static private String _STR_LONG = "LONG";

    final static public String STR_TYP_BLOB = "BLOB";
    final static public String STR_TYP_TINY_BLOB = _STR_TINY + STR_TYP_BLOB;
    final static public String STR_TYP_MEDIUM_BLOB = _STR_MEDIUM + STR_TYP_BLOB;
    final static public String STR_TYP_LONG_BLOB = _STR_LONG + STR_TYP_BLOB;
    final static public String STR_TYP_TEXT = "TEXT";
    final static public String STR_TYP_TINY_TEXT = _STR_TINY + STR_TYP_TEXT ;
    final static public String STR_TYP_MEDIUM_TEXT = _STR_MEDIUM + STR_TYP_TEXT ;
    final static public String STR_TYP_LONG_TEXT = _STR_LONG + STR_TYP_TEXT ;

    final static public String STR_TYP_ENUM = "ENUM";
    final static public String STR_TYP_SET = "SET";
    final static public String STR_TYP_DATE = "DATE";

    public static String getFieldCommand(FldTypAbs fldType){
        if( fldType instanceof FldTypVarChar ){
            return "";
        }
        return null;
    }

     public static String typVarchar(Integer size){
        if (size < MysqlSettings.LENGTH_CHAR_MIN || size > MysqlSettings.LENGHT_CHAR_MAX)
        {
            return "TODO";
        }
        else {
            return TYP_VAR_CHAR +s_sz(size);
        }
    }
    public static String typChar(Integer size){ return TYP_CHAR +s_sz(size);}

    public static String typRefId(String strtable, String strField){
        return TYP_REF_ID + STR_BLANK +strtable +s_intoPar(strField);};

    final static public String TYP_ID = "SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT primary key";
    final static public String TYP_REF_ID = "SMALLINT UNSIGNED NOT NULL REFERENCES";

    /****************************************************************************
   *                 Definition of string for mysql commands                  *
   ****************************************************************************/

    final static public String STR_CMD_INSERT = "INSERT";
    final static public String STR_CMD_SELECT = "SELECT";
    final static public String STR_CMD_CREATE = "CREATE";
    final static public String STR_CMD_COUNT = "COUNT";
    final static public String STR_CMD_DROP = "DROP";
    final static public String STR_CMD_USE = "USE";
    final static public String STR_CMD_SHOW = "SHOW";

   /****************************************************************************
   *                 Definition of string for mysql keywords                  *
   ****************************************************************************/

    final static public String STR_NULL = "NULL";

    final static public String STR_ID_INIT = "1";
    final static private String STR_PAR_RIGHT = "(";
    final static private String STR_PAR_LEFT = ")";
    final static private String STR_COMMA = ",";
    final static private String STR_END = ";";
    final static private String STR_STAR = "*";
    final static private String STR_BLANK = " ";
    final static private String STR_QUOTE = "'";
    final static private String strDB = "DATABASE";
    final static private String STR_TABLE = "TABLE";
    final static private String STR_VALUES = "VALUES";
    final static private String STR_INTO = "INTO";
    final static private String STR_FROM = "FROM";

    final static public String STR_FIELD_SEP = STR_COMMA + STR_BLANK;
    final static private String STR_END_COMMAND =  STR_PAR_LEFT+STR_END;
    final static private String STR_DROP_TABLE = STR_CMD_DROP + STR_BLANK + STR_TABLE + STR_BLANK;
    final static private String STR_CREATE_TBL = STR_CMD_CREATE + STR_BLANK + STR_TABLE + STR_BLANK;
    final static private String STR_CREATE_DTB = STR_CMD_CREATE + STR_BLANK + strDB + STR_BLANK;
    final static private String STR_DROP_DTB = STR_CMD_DROP + STR_BLANK + strDB +STR_BLANK;

    /****************************************************************************
   *                  Definition of string for string utils                   *
   ****************************************************************************/

    private static String s_sz(Integer size){
        return STR_PAR_RIGHT + size.toString() + STR_PAR_LEFT;
    }
    private static String s_intoPar(String str){
        return STR_PAR_RIGHT + str + STR_PAR_LEFT;
    }

}
